package org.apache.http.entity.mime;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.List;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.field.ContentTypeField;
import org.apache.james.mime4j.field.Field;
import org.apache.james.mime4j.message.Body;
import org.apache.james.mime4j.message.BodyPart;
import org.apache.james.mime4j.message.Entity;
import org.apache.james.mime4j.message.Header;
import org.apache.james.mime4j.message.Multipart;
import org.apache.james.mime4j.util.CharsetUtil;

public class HttpMultipart extends Multipart
{
  private HttpMultipartMode mode = HttpMultipartMode.STRICT;

  public HttpMultipart(String paramString)
  {
    super(paramString);
  }

  private void doWriteTo(HttpMultipartMode paramHttpMultipartMode, OutputStream paramOutputStream, boolean paramBoolean)
    throws IOException
  {
    List localList = getBodyParts();
    Charset localCharset = getCharset();
    String str1 = getBoundary();
    BufferedWriter localBufferedWriter = new BufferedWriter(new OutputStreamWriter(paramOutputStream, localCharset), 8192);
    switch (1.$SwitchMap$org$apache$http$entity$mime$HttpMultipartMode[paramHttpMultipartMode.ordinal()])
    {
    default:
    case 1:
    case 2:
    }
    while (true)
    {
      while (true)
      {
        return;
        String str2 = getPreamble();
        if ((str2 != null) && (str2.length() != 0))
        {
          localBufferedWriter.write(str2);
          localBufferedWriter.write("\r\n");
        }
        for (int j = 0; j < localList.size(); ++j)
        {
          localBufferedWriter.write("--");
          localBufferedWriter.write(str1);
          localBufferedWriter.write("\r\n");
          localBufferedWriter.flush();
          BodyPart localBodyPart2 = (BodyPart)localList.get(j);
          localBodyPart2.getHeader().writeTo(paramOutputStream, 1);
          if (paramBoolean)
            localBodyPart2.getBody().writeTo(paramOutputStream, 1);
          localBufferedWriter.write("\r\n");
        }
        localBufferedWriter.write("--");
        localBufferedWriter.write(str1);
        localBufferedWriter.write("--\r\n");
        String str3 = getEpilogue();
        if ((str3 != null) && (str3.length() != 0))
        {
          localBufferedWriter.write(str3);
          localBufferedWriter.write("\r\n");
        }
        localBufferedWriter.flush();
      }
      for (int i = 0; i < localList.size(); ++i)
      {
        localBufferedWriter.write("--");
        localBufferedWriter.write(str1);
        localBufferedWriter.write("\r\n");
        localBufferedWriter.flush();
        BodyPart localBodyPart1 = (BodyPart)localList.get(i);
        localBufferedWriter.write(localBodyPart1.getHeader().getField("Content-Disposition").toString());
        localBufferedWriter.write("\r\n");
        localBufferedWriter.write("\r\n");
        localBufferedWriter.flush();
        if (paramBoolean)
          localBodyPart1.getBody().writeTo(paramOutputStream, 3);
        localBufferedWriter.write("\r\n");
      }
      localBufferedWriter.write("--");
      localBufferedWriter.write(str1);
      localBufferedWriter.write("--\r\n");
      localBufferedWriter.flush();
    }
  }

  protected String getBoundary()
  {
    return ((ContentTypeField)getParent().getHeader().getField("Content-Type")).getBoundary();
  }

  protected Charset getCharset()
  {
    ContentTypeField localContentTypeField = (ContentTypeField)getParent().getHeader().getField("Content-Type");
    Charset localCharset = null;
    switch (1.$SwitchMap$org$apache$http$entity$mime$HttpMultipartMode[this.mode.ordinal()])
    {
    default:
    case 1:
    case 2:
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          return localCharset;
          localCharset = MIME.DEFAULT_CHARSET;
        }
        if (localContentTypeField.getCharset() == null)
          break;
        localCharset = CharsetUtil.getCharset(localContentTypeField.getCharset());
      }
      localCharset = CharsetUtil.getCharset("ISO-8859-1");
    }
  }

  public HttpMultipartMode getMode()
  {
    return this.mode;
  }

  public long getTotalLength()
  {
    List localList = getBodyParts();
    long l1 = 0L;
    int i = 0;
    while (true)
    {
      if (i >= localList.size())
        break label91;
      Body localBody = ((BodyPart)localList.get(i)).getBody();
      if (!(localBody instanceof ContentBody))
        break label83;
      long l3 = ((ContentBody)localBody).getContentLength();
      if (l3 < 0L)
        break;
      l1 += l3;
      ++i;
    }
    long l2 = -1L;
    while (true)
    {
      return l2;
      label83: l2 = -1L;
    }
    label91: ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      doWriteTo(this.mode, localByteArrayOutputStream, false);
      int j = localByteArrayOutputStream.toByteArray().length;
      l2 = l1 + j;
    }
    catch (IOException localIOException)
    {
      l2 = -1L;
    }
  }

  public void setMode(HttpMultipartMode paramHttpMultipartMode)
  {
    this.mode = paramHttpMultipartMode;
  }

  public void writeTo(OutputStream paramOutputStream)
    throws IOException
  {
    doWriteTo(this.mode, paramOutputStream, true);
  }

  public void writeTo(OutputStream paramOutputStream, int paramInt)
    throws IOException, MimeException
  {
    if (paramInt == 3)
      doWriteTo(HttpMultipartMode.BROWSER_COMPATIBLE, paramOutputStream, true);
    while (true)
    {
      return;
      doWriteTo(HttpMultipartMode.STRICT, paramOutputStream, true);
    }
  }
}